package com.pioneer.swing.controller;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JComboBox;
import java.util.List;
import java.util.Iterator;

import com.pioneer.beans.ContactBean;
import com.pioneer.dao.ContactDAO;
import com.pioneer.dao.DataAccessException;
import com.pioneer.swing.ui.*;


/*
 * Author: gchen
 * Created: Saturday, January 03, 2004 9:07:17 AM
 * Modified: Saturday, January 03, 2004 9:07:17 AM
 */


public class ContactsDropDownListener
	implements ActionListener{
	
	private List supplierList;
	private List contractorList;
	private List salesRepList;
	private List regSalesManList;
	private List builderList;
	private JobEntryFrame jobEntryFrame;
	private ContainerEntryFrame containerEntryFrame;
	private ContactDAO contactDAO;
	
	public ContactsDropDownListener() {		
	}
	
	public void setJobEntryFrame(JobEntryFrame frame){
		this.jobEntryFrame = frame;
	}
	
	public void setContainerEntryFrame(ContainerEntryFrame frame){
		this.containerEntryFrame = frame;
	}
	
	public void setContactDAO(ContactDAO contactDAO) {
		this.contactDAO = contactDAO;
	}
	
	public void setSupplierList(List supplierList){
		
		this.supplierList = supplierList;
	}
	
	public void setContractorList(List contractorList){
		this.contractorList = contractorList;
	}	
	
	public void setSalesRepList(List list) {
		this.salesRepList = list;
	}
	
	public void setRegSalesManList(List list) {
		this.regSalesManList = list;
	}
	
	public void setBuilderList(List list){
		this.builderList = list;
	}

	/*
	private ContactBean findContact(String query,List contactList) {
		Iterator contacts = contactList.iterator();
		/*ContactBean returnBean = null;
		boolean found=false;
		while (!found && (contacts.hasNext())) {
			ContactBean curBean = (ContactBean)contacts.next();
			if (query.equals(curBean.getCn())) {
				found=true;
				returnBean = curBean;
			}
		}
		return returnBean;			
		ContactBean foundBean = contactDAO.findContact(query);	
		
	}*/
	
	public void actionPerformed(ActionEvent e) {
		
		try {
	        JComboBox cb = (JComboBox)e.getSource();
	        String selectedItem = (String)cb.getSelectedItem();
	        //updateLabel(petName);
			String category = cb.getName();
		    if (category.equals(UIConstants.DROPDOWN_METALS_SUPPLIER)) {
				//ContactBean salesRep = findContact(selectedItem,salesRepList);
				//if (salesRep!=null) {
					containerEntryFrame.selectedSupplier = selectedItem;
				//}
			
		    } else if (category.equals(UIConstants.DROPDOWN_CONTRACTOR)) {
				ContactBean contractor = contactDAO.findContact(selectedItem);
				if (contractor!=null) {
					if (jobEntryFrame.comboBoxInit) {// this means this is NOT the first time the selection has occured.
						jobEntryFrame.contractorContactTextField.setText(contractor.getFirstName()+" "+contractor.getLastName());
					}
					else{					
						
						jobEntryFrame.comboBoxInit = true;
					}
					jobEntryFrame.contractorPhoneNoTextField.setText(contractor.getBusinessPhone());
					jobEntryFrame.contractorFaxNoTextField.setText(contractor.getBusinessFax());
					jobEntryFrame.contractorAddressTextField.setText(contractor.getBusinessAddress());
					jobEntryFrame.contractorCityTextField.setText(contractor.getBusinessCity());
					jobEntryFrame.contractorStateTextField.setText(contractor.getBusinessState());
					jobEntryFrame.contractorZipTextField.setText(contractor.getBusinessZip());
					jobEntryFrame.selectedContractor = selectedItem;
				}
			} else if (category.equals(UIConstants.DROPDOWN_SUPPLIER)) {
				ContactBean supplier = contactDAO.findContact(selectedItem);
				if (supplier!=null) {
					if (jobEntryFrame.comboBoxInit) {
						jobEntryFrame.supplierContactTextField.setText(supplier.getFirstName()+" "+supplier.getLastName());
						
					}
					else{					
						jobEntryFrame.comboBoxInit = true;
					}
					jobEntryFrame.supplierPhoneNoTextField.setText(supplier.getBusinessPhone());
					jobEntryFrame.supplierFaxNoTextField.setText(supplier.getBusinessFax());
					jobEntryFrame.supplierAddressTextField.setText(supplier.getBusinessAddress());
					jobEntryFrame.supplierCityTextField.setText(supplier.getBusinessCity());
					jobEntryFrame.supplierStateTextField.setText(supplier.getBusinessState());
					jobEntryFrame.supplierZipTextField.setText(supplier.getBusinessZip());
					jobEntryFrame.selectedSupplier = selectedItem;
				}
				
			} else if (category.equals(UIConstants.DROPDOWN_SALESREP)) {
				ContactBean salesRep = contactDAO.findContact(selectedItem);
				if (salesRep!=null) {
					if (jobEntryFrame.comboBoxInit) {
						jobEntryFrame.salesRepContactTextField.setText(salesRep.getFirstName()+" "+salesRep.getLastName());
						
					}
					else{					
						jobEntryFrame.comboBoxInit = true;
					}
					jobEntryFrame.salesRepPhoneNoTextField.setText(salesRep.getBusinessPhone());
					jobEntryFrame.salesRepFaxNoTextField.setText(salesRep.getBusinessFax());
					jobEntryFrame.salesRepAddressTextField.setText(salesRep.getBusinessAddress());
					jobEntryFrame.salesRepCityTextField.setText(salesRep.getBusinessCity());
					jobEntryFrame.salesRepStateTextField.setText(salesRep.getBusinessState());
					jobEntryFrame.salesRepZipTextField.setText(salesRep.getBusinessZip());
					jobEntryFrame.selectedSalesRep = selectedItem;
				}
			} else if (category.equals(UIConstants.DROPDOWN_BUILDER)) {
				ContactBean builder = contactDAO.findContact(selectedItem);
				if (builder!=null) {
					if (jobEntryFrame.comboBoxInit) {
						jobEntryFrame.builderContactTextField.setText(builder.getFirstName()+" "+builder.getLastName());
						
					}
					else{					
						jobEntryFrame.comboBoxInit = true;
					}
					jobEntryFrame.builderPhoneNoTextField.setText(builder.getBusinessPhone());
					jobEntryFrame.builderFaxNoTextField.setText(builder.getBusinessFax());
					jobEntryFrame.builderAddressTextField.setText(builder.getBusinessAddress());
					jobEntryFrame.builderCityTextField.setText(builder.getBusinessCity());
					jobEntryFrame.builderStateTextField.setText(builder.getBusinessState());
					jobEntryFrame.builderZipTextField.setText(builder.getBusinessZip());
					jobEntryFrame.selectedBuilder = selectedItem;
				}
			} else if (category.equals(UIConstants.DROPDOWN_REG_SALES_MAN)) {
				//ContactBean salesRep = findContact(selectedItem,salesRepList);
				//if (salesRep!=null) {
					jobEntryFrame.selectedRegSalesMan = selectedItem;
				//}
			}
			//System.out.println("Action command: "+cb.getName());		
		}
		catch (DataAccessException ex) {
			System.err.println("Could not retrieve the contact info: "+ex.getMessage());
		}
    }

	

}
